package TUILayer;

import ControlLayer.SaleItemCtr;
import ModelLayer.RealException;
import java.util.ArrayList;

public class SaleItemUI {

	private SaleItemCtr saleItemCtr;
	private InputParser inputParser;
	private LoginInfo loginInfo;

	public SaleItemUI(InputParser inputParser, LoginInfo loginInfo) {
		saleItemCtr = new SaleItemCtr();
		this.inputParser = inputParser;
		this.loginInfo = loginInfo;
	}

	public void start() throws RealException {
		int choice = 0;
		boolean exit = false;
		switch (loginInfo.getLoggedEmployeePositionId()) {
		case 1: {
			do {
				try {
					choice = inputParser
							.getInt("\f *** ProductMenu ***\n\n1. Show product information\n2. Create product\n3. Update product\n4. Delete product\n5. List all products\n6. Find product\n7. Return\n\nMake your choice: ");
					switch (choice) {
					case 1:
						showSaleItemInfo();
						break;
					case 2:
						createSaleItem();
						break;
					case 3:
						updateSaleItem();
						break;
					case 4:
						deleteSaleItem();
						break;
					case 5:
						listAllSaleItems();
						break;
					case 6:
						findSaleItem();
						break;
					case 7:
						exit = true;
						break;
					}
				} catch (RealException e) {
					System.out.println("\nSomething went wrong!\n"
							+ e.getError() + " Please try again");
					inputParser.printReturn();
				}
			} while (!exit);
			break;
		}
		case 2: {
			do {
				try {
					choice = inputParser
							.getInt("\f *** ProductMenu ***\n\n1. Show product information\n2. Create product\n3. Update product\n4. Delete product\n 5. List all products\n6. Find product\n7. Return\n\nMake your choice: ");
					switch (choice) {
					case 1:
						showSaleItemInfo();
						break;
					case 2:
						createSaleItem();
						break;
					case 3:
						updateSaleItem();
						break;
					case 4:
						deleteSaleItem();
						break;
					case 5:
						listAllSaleItems();
						break;
					case 6:
						findSaleItem();
						break;
					case 7:
						exit = true;
						break;
					}
				} catch (RealException e) {
					System.out.println("\nSomething went wrong!\n"
							+ e.getError() + " Please try again");
					inputParser.printReturn();
				}
			} while (!exit);
			break;
		}
		case 3: {
			do {
				try {
					choice = inputParser
							.getInt("\f *** ProductMenu ***\n\n1. Show product information\n2. Update product stock\n3. List all products\n4. Find product\n5. Return\n\nMake your choice: ");
					switch (choice) {
					case 1:
						showSaleItemInfo();
						break;
					case 2: {
						String barcode = inputParser
								.getString("\f ***UpdateProductMenu ***\n\nEnter the products barcode: ");
						boolean found = true;
						try {
							saleItemCtr.checkSaleItem(barcode);
						} catch (RealException e) {
							found = false;
							System.out
									.println("\f ***UpdateProductMenu ***\n\n"
											+ e.getError());
							inputParser.printReturn();
						}
						if (found) {
							String value = String.valueOf(inputParser
									.getInt("\f ***UpdateProductMenu ***\n\nPlease enter the new product current stock: "));
							saleItemCtr.updateSaleItem(barcode, 8, value);
						}
						break;
					}
					case 3:
						listAllSaleItems();
						break;
					case 4:
						findSaleItem();
						break;
					case 5:
						exit = true;
						break;
					}
				} catch (RealException e) {
					System.out.println("\nSomething went wrong!\n"
							+ e.getError() + " Please try again");
					inputParser.printReturn();
				}
			} while (!exit);
			break;
		}
		}
	}

	public void showSaleItemInfo() throws RealException {
		String barcode = inputParser
				.getString("\f ***ShowProductMenu ***\n\n Please enter product barcode: ");
		System.out.println("\f ***CreateProductMenu ***\n\n");
		System.out.println(saleItemCtr.getSaleItemInfo(barcode));
		inputParser.printReturn();
	}

	public void createSaleItem() throws RealException {
		String name = inputParser
				.getString("\f ***CreateProductMenu ***\n\nEnter the products name: ");
		double bPrice = inputParser
				.getDouble("\f ***CreateProductMenu ***\n\nEnter the products buy price: ");
		double sPrice = inputParser
				.getDouble("\f ***CreateProductMenu ***\n\nEnter the products sell price: ");
		double tax = inputParser
				.getPercent("\f ***CreateProductMenu ***\n\nEnter the products tax (%): ");
		String um = inputParser
				.getString("\f ***CreateProductMenu ***\n\nEnter the products um: ");
		int minStock = inputParser
				.getInt("\f ***CreateProductMenu ***\n\nEnter the products minimum stock: ");
		int maxStock = inputParser
				.getInt("\f ***CreateProductMenu ***\n\nEnter the products maximum stock: ");
		int currStock = inputParser
				.getInt("\f ***CreateProductMenu ***\n\nEnter the products current stock: ");
		int discountQ = inputParser
				.getInt("\f ***CreateProductMenu ***\n\nEnter the products discount quantity: ");
		int location = inputParser
				.getInt("\f ***CreateProductMenu ***\n\nEnter the products location number: ");
		int contractorID = inputParser
				.getInt("\f ***CreateProductMenu ***\n\nEnter the products contracor ID: ");

		saleItemCtr.createSaleItem(name, location, bPrice, sPrice, tax, um,
				minStock, maxStock, currStock, discountQ, contractorID);
		inputParser.printReturn();
	}

	public void updateSaleItem() throws RealException {
		String barcode = inputParser
				.getString("\f ***UpdateProductMenu ***\n\nEnter the products barcode: ");
		int choice;
		boolean found = true;
		try {
			saleItemCtr.checkSaleItem(barcode);
		} catch (RealException e) {
			found = false;
			System.out
					.println("\f ***UpdateProductMenu ***\n\n" + e.getError());
			inputParser.printReturn();
		}
		if (found) {
			String value = new String();
			boolean exit = false;
			do {
				choice = inputParser
						.getInt("\f ***UpdateProductMenu ***\n\n1. Change name\n2. Change buy price\n3. Change sell price\n4. Change tax\n5. Change um\n6. Change minumum stock\n7. Change maximum stock\n8. Change current stock\n9. Change discount quantity\n10. Change location\n11. Change contractor\n12. Return\n\nMake your choice: ");
				switch (choice) {
				case 1:
					value = inputParser
							.getString("\f ***UpdateProductMenu ***\n\nPlease enter the new product name: ");
					break;
				case 2:
					value = String.valueOf(inputParser
							.getDouble("\f ***UpdateProductMenu ***\n\nPlease enter the new product buy price: "));
					break;
				case 3:
					value = String.valueOf(inputParser
							.getDouble("\f ***UpdateProductMenu ***\n\nPlease enter the new product sell price: "));
					break;
				case 4:
					value = String.valueOf(inputParser
							.getPercent("\f ***UpdateProductMenu ***\n\nPlease enter the new product tax (%): "));
					break;
				case 5:
					value = inputParser
							.getString("\f ***UpdateProductMenu ***\n\nPlease enter the new product um: ");
					break;
				case 6:
					value = String.valueOf(inputParser
							.getInt("\f ***UpdateProductMenu ***\n\nPlease enter the new product minimum stock: "));
					break;
				case 7:
					value = String.valueOf(inputParser
							.getInt("\f ***UpdateProductMenu ***\n\nPlease enter the new product maximum stock: "));
					break;
				case 8:
					value = String.valueOf(inputParser
							.getInt("\f ***UpdateProductMenu ***\n\nPlease enter the new product current stock: "));
					break;
				case 9:
					value = String.valueOf(inputParser
							.getInt("\f ***UpdateProductMenu ***\n\nPlease enter the new product discount quantity: "));
					break;
				case 10:
					value = String.valueOf(inputParser
							.getInt("\f ***UpdateProductMenu ***\n\nPlease enter the new product location: "));
					break;
				case 11:
					value = String.valueOf(inputParser
							.getInt("\f ***UpdateProductMenu ***\n\nPlease enter the new product contractor ID: "));
					break;
				case 12:
					exit = true;
					break;
				}
				if (choice >= 1 && choice <= 11) {
					saleItemCtr.updateSaleItem(barcode, choice, value);
					System.out
							.println("\f ***UpdateProductMenu ***\n\nProduct updated.");
					inputParser.printReturn();
				}
			} while (!exit);
		}
	}

	public void deleteSaleItem() throws RealException {
		String barcode = inputParser
				.getString("\f ***DeleteProductMenu ***\n\nPlease input the product barcode: ");
		saleItemCtr.deleteSaleItem(barcode);
	}

	public void listAllSaleItems() {
		ArrayList<String> saleItemsInfo = saleItemCtr.getAllSaleItemInfo();
		System.out.println("\f ***ListAllProducts ***\n");
		for (String saleIntemInfo : saleItemsInfo) {
			System.out.println(saleIntemInfo);
		}
		inputParser.printReturn();
	}

	public void findSaleItem() throws RealException {
		String searchPattern = inputParser
				.getString("\f ***FindProductMenu ***\n\nPase enter start of the product name you are searching for: ");
		ArrayList<String> matchingSaleItems = saleItemCtr
				.findSaleItem(searchPattern);
		System.out.println("\f ***MatchingProducts ***\n");
		for (String saleItemInfo : matchingSaleItems) {
			System.out.println(saleItemInfo);
		}
		inputParser.printReturn();
	}
}
